﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class FrequencyDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Frequency.GetAll);
		}

		public static DataTable GetOne(string frequencyID)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@FrequencyID", SqlDbType.NVarChar, frequencyID)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Frequency.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static Frequency LoadOne(string frequencyID)
		{
			Frequency o = new Frequency();
			DataTable dt = GetOne(frequencyID);
			if (dt.Rows.Count > 0)
			{
				o.FrequencyID = dt.Rows[0][ToolConstants.Entities.Frequency.FieldName.FrequencyID].ToString();
				o.FrequencyName = dt.Rows[0][ToolConstants.Entities.Frequency.FieldName.FrequencyName].ToString();
				o.FrequencyName_English = dt.Rows[0][ToolConstants.Entities.Frequency.FieldName.FrequencyName_English].ToString();

			}

			return o;
		}


		public static int Insert(Frequency o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@FrequencyID", SqlDbType.NVarChar, o.FrequencyID),
                        CreateParameter("@FrequencyName", SqlDbType.NVarChar, o.FrequencyName),
                        CreateParameter("@FrequencyName_English", SqlDbType.NVarChar, o.FrequencyName_English),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Frequency.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(Frequency o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@FrequencyID", SqlDbType.NVarChar, o.FrequencyID),
                        CreateParameter("@FrequencyName", SqlDbType.NVarChar, o.FrequencyName),
                        CreateParameter("@FrequencyName_English", SqlDbType.NVarChar, o.FrequencyName_English),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Frequency.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(Frequency o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@FrequencyID", SqlDbType.NVarChar, o.FrequencyID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Frequency.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_Frequency");
			table.Columns.Add("FrequencyID");
			table.Columns.Add("FrequencyName");
			table.Columns.Add("FrequencyName_English");

			return table;
		}

		#endregion
	}
}
